package serial;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Box implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int width; 
	private int height;
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	@Override
	public String toString() {
		return "Box [width=" + width + ", height=" + height + "]";
	} 
	
	public static void main(String[] args) {
		serial();
		un1serial();
	}
	
	
	public static void serial(){
		Box myBox = new Box(); 
		myBox.setWidth(50); 
		myBox.setHeight(30); 
		try{ 
		FileOutputStream fs = new FileOutputStream("F:\\foo.ser"); 
		ObjectOutputStream os = new ObjectOutputStream(fs); 
		os.writeObject(myBox); 
		os.close(); 
		}catch(Exception ex){ 
		ex.printStackTrace(); 
		} 
	}
	
	public static void un1serial(){
		
		
		    try {
		    	FileInputStream byteArrayInputStream = new FileInputStream("F:\\foo.ser");
		    	ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
				Box object = (Box) objectInputStream.readObject();
				objectInputStream.close();
				byteArrayInputStream.close();
				
				System.out.println(object.toString());
			} catch (ClassNotFoundException | IOException e) {
				e.printStackTrace();
			}
		
		
		
	}	
}
